
bdf.Report.prototype.generatePdfGridReport=function(view,gridIds,configData,onlineView,dataScope,maxSize){
	var datagrid = view.get('#'+gridIds);
	var list = datagrid.getBindingData();
	var __Entity=datagrid.__Entity;
	if(!__Entity){
	 __Entity =datagrid.__Entity= list.createChild({}, true);
	}
	if(!configData)configData={};
	var grid = this.buildExcelGridData(view, gridIds, configData,dataScope,maxSize);
	var parameter = {
			titleInfos: configData.titleInfos,
			grid: grid,
			__Entity: __Entity
		};
	var action=new dorado.widget.AjaxAction();
	action.set("parameter",parameter);
	action.set("executingMessage",$resource("executingMessage"));
	action.set("service","bdf.pdfReportController#buildPdfGridReportData");

	action.execute(function(fileName){
		var url=$url(">bdf.reportController.downloadReport.c?fileName="+fileName+"&type=pdf");
		if(onlineView){
			var dialog=new dorado.widget.Dialog({
				width:750,
				height:600,
				center:true,
				modal:true,
				maximizeable:true,
				closeAction:"close",
				children:[{
					$type:"HtmlContainer",
					contentOverflow:"hidden",
					content:"<div id='_swfReportViewerDiv'>"
				}]
			});
			dialog.show();
			var viewer=new bdf.SwfViewer();
			var config={
				swf:$url(">bdf.pdfReportController.generateSwf.c?sourcePdf="+fileName),
				divId:"_swfReportViewerDiv",
				isJasperReports:false,
				downloadReportUrl:url
			};
			viewer.createView(config);	
		}else{
			window.open(url, "_blank");
			/*if(dorado.Browser.msie || isChromeFrame()){
				//window.location.href=url;
				window.open(url, "_blank");
			}else{
				var $dowloadIframe = view.$dowloadIframe;
				if(!$dowloadIframe){
					$dowloadIframe =jQuery("<iframe style='width:0,height:0'></iframe>").appendTo(jQuery(document.body));
					view.$dowloadIframe = $dowloadIframe;
				}
				setTimeout(function(){
					$dowloadIframe.prop("src", url);
				},0);
			}
			*/
		}
			
	});
};

bdf.Report.prototype.generateExcelGridReport=function(view,gridIds,configData,dataScope,maxSize){
	var datagrid = view.get('#'+gridIds);
	var list = datagrid.getBindingData();
	var __Entity=datagrid.__Entity;
	if(!__Entity){
	 __Entity =datagrid.__Entity= list.createChild({}, true);
	}
	if(!configData)configData={};
	var action=new dorado.widget.AjaxAction();
	action.set("service","bdf.excelReportController#buildExcelGridReportData");
	action.set("executingMessage",$resource("executingMessage"));
	var grid = this.buildExcelGridData(view, gridIds, configData,dataScope,maxSize);
	var parameter = {
			titleInfos: configData.titleInfos,
			grid: grid,
			__Entity: __Entity
		};
	action.set("parameter",parameter);
	action.execute(function(fileName){
		var url="bdf.reportController.downloadReport.c?fileName="+fileName+"&type=xls";
		window.open(url, "_blank");
		/*if(dorado.Browser.msie || isChromeFrame()){
			//window.location.href=url;
			window.open(url, "_blank");
		}else{
			var $dowloadIframe = view.$dowloadIframe;
			if(!$dowloadIframe){
				$dowloadIframe =jQuery("<iframe style='width:0,height:0'></iframe>").appendTo(jQuery(document.body));
				view.$dowloadIframe = $dowloadIframe;
			}
			setTimeout(function(){
				$dowloadIframe.prop("src", url);
			},0);
		}*/
	});
};


gdgs.ndp.report={
		generateExcelGridReport:function(view,gridId,configData,dataScope,maxSize){
			if(!configData)configData={};
			var action=new dorado.widget.AjaxAction();
			action.set("service","gdgs.base.exportController#buildExcelGridReportData");
			var datagrid = view.get('#'+gridId);
			var list = datagrid.getBindingData();
			var __Entity=datagrid.__Entity;
			if(!__Entity){
			 __Entity =datagrid.__Entity= list.createChild({}, true);
			}
			var grid = this.buildExcelGridData(view, gridId, configData,dataScope,maxSize);
			var parameter = {
					titleInfos : configData.titleInfos,
					grid : grid,
					__Entity:__Entity
			};
			action.set("parameter",parameter);
			action.execute(function(fileName){
				var url="gdgs.base.exportController.downloadReport.c?fileName="+fileName+"&type=xls";
				var dialog =view.$_progressDia;
				if(dialog){
					dialog.hide();
				}
				window.open(url, "_blank");
				/*if(dorado.Browser.msie || isChromeFrame()){
					//window.location.href=url;
					window.open(url, "_blank");
				}else{
					var $excelIframe = view.$excelIframe;
					if(!$excelIframe){
						$excelIframe =jQuery("<iframe style='width:0,height:0'></iframe>").appendTo(jQuery(document.body));
						view.$excelIframe = $excelIframe;
					}
					setTimeout(function(){
						$excelIframe.prop("src", url);
					},0);
				}
				*/
			});
		},
		buildExcelGridData : function(view, gridId,config,dataScope,maxSize) {
			
			var grid=view.get('#'+gridId);
			var dataPath=grid.get("dataPath");
			var ds=grid.get("dataSet");
			var dataType=ds.get("data").elementDataType;
			if(dataPath){
				dataType=ds.getData(dataPath).elementDataType;
			}
			var gridInfos =  this.buildSingleGridData(view,gridId,dataType,config,dataScope,maxSize);
			var grid = {
					gridInfos : gridInfos
				};
			return grid;
		},
		buildSingleGridData:function(view,gridId,dataType,config,dataScope,maxSize){
			if(dataScope && dataScope!="currentPage" && dataScope!="serverAll"){
				throw new dorado.Exception("Don't support the scope:"+dataScope);
			}
			var grid = view.get('#'+gridId);
			var dataPath=grid.get("dataPath");
			var columns = grid.get("columns");
			var ds = grid.get("dataSet");
			var resultList = new dorado.util.KeyedList();
			this._buildColumnsInfo(columns,dataType,null, resultList, config.columnHeaderStyle,dataScope);
			var result = new dorado.util.Map();
			result.put("columnInfos", resultList.toArray());
			result.put("dataScope", dataScope);		
			result.put("maxSize",maxSize);		
			result.put("gridId",gridId);
			var data = null;
			if (dataPath) {
				data = ds.getData(dataPath);
			} else {
				data = ds.getData();
			}
			result.put("entityCount",data.entityCount)
			if(!dataScope || dataScope=="currentPage"){
				//retrive current page data
				var iter = data.iterator({
					currentPage : true
				});
				iter.first();
				var resultArray = new Array();
				while (iter.hasNext()) {
					var entity=iter.next();
					var json=this._retrieveComplexObjectValue(entity.toJSON(),entity);
					resultArray.push(json);
				}
				result.put("data", resultArray);		
			}else{
				var dialog=view.$_progressDia;
				if(!dialog){
					dialog =new dorado.widget.FloatPanel({
						width:this.width,
						height:this.height,
						caption:this.caption,
						center:true,
						modal:false,
						maximizeable:true,
						closeable:true,
						maximizeable:false,
						closeAction:"hide",
						listener:{
							onShow:function(self,arg){
								var action=new dorado.widget.AjaxAction();
								action.set("parameter",{gridId:gridId});
								action.set("service","gdgs.base.exportController#getProgress");
								var progress =view.get('#'+"progressBar"+gridId);
								if(progress){
									progress.set({
										maxValue:1,
										value:0
									});
								}
								var callback =function(rs){
									if(rs){
										progress.set({
											maxValue:1,
											value:rs
										});
									}
									if(1 != rs|| !rs){
										setTimeout(function(){ 
											action.execute(callback); 
										}, 800);
									}else{
										dialog.hide();
									}
								}
								action.execute(callback);
							}
						},
						children:[{
							$type:"ProgressBar",
							id:"progressBar"+gridId,
							width:300,
							maxValue:1,
							value:0
						}]
					});
					view.addChild(dialog);
					view.$_progressDia = dialog;
				}
				dialog.show();
				
				//retrive server side all data
				var retriveResult=this._retriveReferenceParameter(ds,dataPath);
				if(retriveResult.isRef){
					result.put("dataProviderParameter",retriveResult.parameter);
					result.put("pageSize",retriveResult.pageSize);
					result.put("dataProviderId",retriveResult.propertyDef.get("dataProvider.id"));
				}else{
					result.put("dataProviderId", ds.get("dataProvider").name);		
					result.put("dataProviderParameter",ds.get("parameter"));
					result.put("pageSize", ds.get("pageSize") || 0);			
				}
			}
			result.put("gridDataStyle",config.gridDataStyle);
			return result;
		},
		_retrieveComplexObjectValue:function(json,entity){
			var map = new dorado.util.Map();
			for(var propertyName in json){
				var data=entity.get(propertyName);
				map.put(propertyName, data);
				var iscomplex=false;
				if(data!=null  && !(data instanceof dorado.EntityList)){
				    var dt=data.dataType;
				    if(dt){
				    	var displayProperty=dt.get("defaultDisplayProperty");
				    	if(displayProperty){
				    		map.put(propertyName, data.get(displayProperty));
				    		iscomplex=true;
				    	}
				    }
				}
				if(!iscomplex){
					var dt=entity.dataType;
					if(dt){
						dt.get("propertyDefs").each(function(propertyDef){
							var go=true;
							var name=propertyDef.get("name");
							if(name==propertyName){
								var mappings=propertyDef.get("mapping");
								if(mappings){
									for(var i=0;i<mappings.length;i++){
										var mapping=mappings[i];
										var mappingKey=mapping.key;
										if(data!=null && (mappingKey==data.toString())){
											data=mapping.value;
											go=false;
											break;
										}
									}
								}
							}
							return go;
						});
					}
					map.put(propertyName, data);
				}
			}
			return map.toJSON();
		},
		_retriveReferenceParameter:function(dataSet,dataPath){
			var supported=false, propertyDef=null, parameter={},pageSize = 0;
			if (dataPath && dataPath.match(/\.[\#\w]*$/)) {
				var i = dataPath.lastIndexOf('.');
				var parentDataPath = dataPath.substring(0, i), subProperty = dataPath.substring(i + 1);
				if (subProperty.charAt(0) == '#') subProperty = subProperty.substring(1);		
				var parentEntity = dataSet.getData(parentDataPath);
				if (parentEntity && parentEntity instanceof dorado.Entity) {
					var parentDataType = parentEntity.dataType;
					if (parentDataType && parentDataType instanceof dorado.EntityDataType) {
						propertyDef = parentDataType.getPropertyDef(subProperty);
						if (propertyDef && propertyDef instanceof dorado.Reference) {
							dorado.$this = parentEntity;
							parameter = dorado.JSON.evaluate(propertyDef.get("parameter"));
							pageSize = propertyDef.get("pageSize");
							supported = true;
						}
					}
				}
			}
			if(!supported && dataPath){
				throw new dorado.Exception($resource("supportExceptionPrefix")+dataPath+$resource("supportExceptionSuffix"));
			}
			return {isRef:supported,propertyDef:propertyDef,parameter:parameter,pageSize: pageSize || 0};
		},
		_buildColumnsInfo:function(childrenColumns,dataType,parentMap, resultList, columnHeaderStyle,dataScope){
			var columns = childrenColumns.toArray();
			for ( var i = 0; i < columns.length; i++) {
				var col = columns[i];
				if (col instanceof dorado.widget.grid.IndicatorColumn) {
					continue;
				}
				if (col instanceof dorado.widget.grid.RowNumColumn) {
					continue;
				}
				if (col instanceof dorado.widget.grid.RowSelectorColumn) {
					continue;
				}
				if (!col.get("visible")) {
					continue;
				}
				var columnName;
				if (col instanceof dorado.widget.grid.ColumnGroup) {
					columnName = col.get("name");
				} else {
					columnName = col.get("property");
				}

				var columnCaption = col.get("caption");
				var width = col._realWidth;
				if (width == undefined) {
					width = 0;
				}
				var level = this._generateHeaderLevel(col, 1);
				var map = new dorado.util.Map();
				map.put("columnName", columnName);
				map.put("level", level);
				map.put("label", columnCaption);
				map.put("width", width);
				var mapping=this._retriveGridColumnMapping(columnName,dataType);
				if(mapping){
					map.put(columnName+"_mapping",mapping);
				}
				if(dataScope && (dataScope=="serverAll")){
					var displayProperty=this._retriveEntityDisplayProperty(columnName,dataType);
					if(displayProperty){
						map.put(columnName+"_displayProperty",displayProperty);
					}			
				}
				
				if (columnHeaderStyle) {
					map.put("bgColor", columnHeaderStyle.headerBgColor);
					map.put("fontColor", columnHeaderStyle.headerFontColor);
					map.put("fontSize", columnHeaderStyle.headerFontSize);
					map.put("align", columnHeaderStyle.headerAlign);
				}
				
				if (col instanceof dorado.widget.grid.ColumnGroup) {
					if (col.get("columns").toArray().length > 0) {
						this._buildColumnsInfo(col.get("columns"),dataType,map,
								new dorado.util.KeyedList(), columnHeaderStyle,dataScope);
					}
				}
				resultList.insert(map.toJSON());
			}
			if (parentMap != null) {
				parentMap.put("children", resultList.toArray());
			}
		},
		_retriveEntityDisplayProperty:function(){
			var displayProperty=null;
			if(!(dataType instanceof dorado.EntityDataType)){
				return displayProperty;
			}
			var propertyDefs=dataType.get("propertyDefs");
			if(propertyDefs instanceof dorado.util.KeyedArray){
				propertyDefs.each(function(propertyDef){
					if(propertyDef.get("name")==columnName){
						var dt=propertyDef.get("dataType");
						if(dt && (dt instanceof dorado.EntityDataType)){
							displayProperty=dt.get("defaultDisplayProperty");					
						}
						return false;
					}
				});
			}	
			return displayProperty;
		},
		_retriveGridColumnMapping:function(columnName,dataType){
			var mapping=null;
			if(!(dataType instanceof dorado.EntityDataType)){
				return mapping;
			}
			var propertyDef=dataType.getPropertyDef(columnName) ;
			if(propertyDef){
				mapping=propertyDef.get("mapping");
			}
			return mapping;
		},
		_generateHeaderLevel:function (col, level) {
			var parentColumn = col.get("parent");
			if (parentColumn instanceof dorado.widget.DataGrid) {
				return level;
			}
			if (parentColumn instanceof dorado.widget.grid.ColumnGroup) {
				level++;
				return this._generateHeaderLevel(parentColumn, level);
			}
			return level;
		}
}